﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace XamlCleaner.Core.Loader
{
    /// <summary>
    /// Load XmlDocument from different sources
    /// </summary>
    public class XamlLoader : IXamlLoader, IDisposable
    {
        #region Members
        XmlDocument document;
        #endregion

        #region Constructors
        /// <summary>
        /// Load XmlDocument from file path
        /// </summary>
        /// <param name="filePath">File path to load</param>
        public XamlLoader(string filePath)
        {
            this.document = new XmlDocument();
            try
            {
                this.document.Load(filePath);
            }
            catch (Exception)
            {
                this.document = null;
            }

        }

        /// <summary>
        ///  Load XmlDocument from Stream
        /// </summary>
        /// <param name="stream">The stream containing the XML document to load.</param>
        public XamlLoader(Stream stream)
        {
            this.document = new XmlDocument();
            try
            {
                this.document.Load(stream);
            }
            catch (Exception)
            {
                this.document = null;
            }

        }

        /// <summary>
        /// Loads the XML document from the specified System.IO.TextReader.
        /// </summary>
        /// <param name="txtReader">The TextReader used to feed the XML data into the document.</param>
        public XamlLoader(TextReader txtReader)
        {
            this.document = new XmlDocument();
            try
            {
                this.document.Load(txtReader);
            }
            catch (Exception)
            {
                this.document = null;
            }
            
        }

        /// <summary>
        /// Loads the XML document from the specified System.Xml.XmlReader.
        /// </summary>
        /// <param name="reader">The XmlReader used to feed the XML data into the document.</param>
        public XamlLoader(XmlReader reader)
        {
            this.document = new XmlDocument();
            try
            {
                this.document.Load(reader);
            }
            catch (Exception)
            {
                this.document = null;
            }

        }
        #endregion

        #region IXamlLoader Properties
        /// <summary>
        /// The XamlDocument loaded or null if there is an error.
        /// </summary>
        public System.Xml.XmlDocument Document
        {
            get { return this.document; }
        }
        #endregion

        #region IDisposable Methods

        public void Dispose()
        {
            this.document = null;
        }

        #endregion
    }
}
